Skip to contentSkip to conversation

Change in openvpn[master]: Make 'lport 0' no longer sufficient to do '--bind'.

Openvpn-devel In review cron2 (Code Review) 2 participants 7 messages Mar 24, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1742829972000.I1976307a7643c82f31d55ca32c79cbe64b6fffc6@...2715...

#1 lore raw

Attention is currently required from: flichtenheld, plaisthos.

Hello plaisthos, flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/916?usp=email

to review the following change.

Change subject: Make 'lport 0' no longer sufficient to do '--bind'.
......................................................................

Make 'lport 0' no longer sufficient to do '--bind'.

'lport <anything>' used to trigger 'do socket bind', which is not useful in itself for the 'lport 0' case (port 0 -> OS assigns a random port, as it is done for unbound sockets) unless also binding to a particular local IP address ('--local 192.0.2.1').

The trigger for 'lport has been used, do socket bind' is ce.local_port_defined -> change the code to test for "0", and only set this for non-0 ports (NOTE: this is a string match, so if you really really want the old "lport 0" behaviour, using "lport 00" still does that...).

The ce.local_port value is still set, so '--lport 0' together with '--local 192.0.2.1' will give you a random port number bound to that IP address - without 'lport 0' it would default to 1194 or the value of '--port' (if not using '--rport').

Summary: socket bind is now only done if one of these is set
- --port <port> with <port> not "0"
- --bind (default on the client is "--nobind")
- --local <address>

Change-Id I1976307a7643c82f31d55ca32c79cbe64b6fffc6

---
M doc/man-sections/link-options.rst
M src/openvpn/options.c

2 files changed, 9 insertions(+), 2 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/16/916/1

doc/man-sections/link-options.rst +3 -1
index d48021e..287473e 100644
@@ -122,7 +122,9 @@
 
 --lport port
   Set default TCP/UDP port number. Cannot be used together with
-  ``--nobind`` option.
+  ``--nobind`` option.  A port number of ``0`` is only honoured to
+  achieve "bind() to a random assigned port number" if a bind-to IP
+  address is specified with ``--local``.
 
 --mark value
   Mark encrypted packets being sent with ``value``. The mark value can be
src/openvpn/options.c +6 -1
index ab56609..99dd60a 100644
@@ -6710,7 +6710,12 @@
     else if (streq(p[0], "lport") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
-        options->ce.local_port_defined = true;
+
+        /* only trigger bind() if port is not 0 (or --local is used) */
+        if (!streq(p[1], "0"))
+        {
+            options->ce.local_port_defined = true;
+        }
         options->ce.local_port = p[1];
     }
     else if (streq(p[0], "rport") && p[1] && !p[2])

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/916?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1976307a7643c82f31d55ca32c79cbe64b6fffc6
Gerrit-Change-Number: 916
Gerrit-PatchSet: 1
Gerrit-Owner: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: cron2, flichtenheld.

plaisthos has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/916?usp=email )

Change subject: Make 'lport 0' no longer sufficient to do '--bind'.
......................................................................

Patch Set 1: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/916?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1976307a7643c82f31d55ca32c79cbe64b6fffc6
Gerrit-Change-Number: 916
Gerrit-PatchSet: 1
Gerrit-Owner: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Mon, 24 Mar 2025 15:45:59 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: cron2, flichtenheld.

ordex has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/916?usp=email )

Change subject: Make 'lport 0' no longer sufficient to do '--bind'.
......................................................................

Patch Set 1:

(1 comment)

File src/openvpn/options.c:

http://gerrit.openvpn.net/c/openvpn/+/916/comment/e5ea9251_3915c842 :
PS1, Line 6719: options->ce.local_port = p[1];
what's the purpose of keeping this line outside the ifblock?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/916?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1976307a7643c82f31d55ca32c79cbe64b6fffc6
Gerrit-Change-Number: 916
Gerrit-PatchSet: 1
Gerrit-Owner: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: ordex <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Mon, 24 Mar 2025 16:47:20 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

#4 lore raw

PATCH 1/1 Make 'lport 0' no longer sufficient to do '--bind'.

In reply to a message not in the archive

'lport <anything>' used to trigger 'do socket bind', which is not useful in itself for the 'lport 0' case (port 0 -> OS assigns a random port, as it is done for unbound sockets) unless also binding to a particular local IP address ('--local 192.0.2.1').

The trigger for 'lport has been used, do socket bind' is ce.local_port_defined -> change the code to test for "0", and only set this for non-0 ports (NOTE: this is a string match, so if you really really want the old "lport 0" behaviour, using "lport 00" still does that...).

The ce.local_port value is still set, so '--lport 0' together with '--local 192.0.2.1' will give you a random port number bound to that IP address - without 'lport 0' it would default to 1194 or the value of '--port' (if not using '--rport').

Summary: socket bind is now only done if one of these is set
- --port <port> with <port> not "0"
- --bind (default on the client is "--nobind")
- --local <address>

Change-Id I1976307a7643c82f31d55ca32c79cbe64b6fffc6

---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/916
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Arne Schwabe <[email protected]...>

doc/man-sections/link-options.rst +3 -1
index d48021e..287473e 100644
@@ -122,7 +122,9 @@
 
 --lport port
   Set default TCP/UDP port number. Cannot be used together with
-  ``--nobind`` option.
+  ``--nobind`` option.  A port number of ``0`` is only honoured to
+  achieve "bind() to a random assigned port number" if a bind-to IP
+  address is specified with ``--local``.
 
 --mark value
   Mark encrypted packets being sent with ``value``. The mark value can be
src/openvpn/options.c +6 -1
index ab56609..99dd60a 100644
@@ -6710,7 +6710,12 @@
     else if (streq(p[0], "lport") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
-        options->ce.local_port_defined = true;
+
+        /* only trigger bind() if port is not 0 (or --local is used) */
+        if (!streq(p[1], "0"))
+        {
+            options->ce.local_port_defined = true;
+        }
         options->ce.local_port = p[1];
     }
     else if (streq(p[0], "rport") && p[1] && !p[2])
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: Make 'lport 0' no longer sufficient to do '--bind'.

Tested this with various combinations of --port, --bind, --lport, --local <v4|v6>, etc. - a fascinating world of interesting effects.

I've fixed a small oversight in the commit message - the line needs to read

Summary: socket bind is now only done if one of these is set
- --lport <port> with <port> not "0"

(with "--lport", as "--port" never leads to an automatic bind)

Also added a reference to GH schwabe/ics-openvpn#1794 where half the problems go away if "lport 0" is removed from the config - which, in these cases, translates to "is not enabling --bind" (the real issue is "any" bind AF vs. getaddrinfo(), but not binding at all helps).

Patch has been applied to the master branch.

commit c91948a0e03f0ad03e7fdde59ed9fce87ba00885

Author: Gert Doering
Date:   Mon Mar 24 19:27:26 2025 +0100
     Make 'lport 0' no longer sufficient to do '--bind'.

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg31222.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#6 lore raw
In reply to a message not in the archive

Hello flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/916?usp=email

to look at the new patch set (#2).

The following approvals got outdated and were removed:
Code-Review+2 by plaisthos

Change subject: Make 'lport 0' no longer sufficient to do '--bind'.
......................................................................

Make 'lport 0' no longer sufficient to do '--bind'.

'lport <anything>' used to trigger 'do socket bind', which is not useful in itself for the 'lport 0' case (port 0 -> OS assigns a random port, as it is done for unbound sockets) unless also binding to a particular local IP address ('--local 192.0.2.1').

The trigger for 'lport has been used, do socket bind' is ce.local_port_defined -> change the code to test for "0", and only set this for non-0 ports (NOTE: this is a string match, so if you really really want the old "lport 0" behaviour, using "lport 00" still does that...).

The ce.local_port value is still set, so '--lport 0' together with '--local 192.0.2.1' will give you a random port number bound to that IP address - without 'lport 0' it would default to 1194 or the value of '--port' (if not using '--rport').

Summary: socket bind is now only done if one of these is set
- --lport <port> with <port> not "0"
- --bind (default on the client is "--nobind")
- --local <address>

Github: schwabe/ics-openvpn#1794

Change-Id I1976307a7643c82f31d55ca32c79cbe64b6fffc6

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg31222.html

---
M doc/man-sections/link-options.rst
M src/openvpn/options.c

2 files changed, 9 insertions(+), 2 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/16/916/2

doc/man-sections/link-options.rst +3 -1
index d48021e..287473e 100644
@@ -122,7 +122,9 @@
 
 --lport port
   Set default TCP/UDP port number. Cannot be used together with
-  ``--nobind`` option.
+  ``--nobind`` option.  A port number of ``0`` is only honoured to
+  achieve "bind() to a random assigned port number" if a bind-to IP
+  address is specified with ``--local``.
 
 --mark value
   Mark encrypted packets being sent with ``value``. The mark value can be
src/openvpn/options.c +6 -1
index ab56609..99dd60a 100644
@@ -6710,7 +6710,12 @@
     else if (streq(p[0], "lport") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
-        options->ce.local_port_defined = true;
+
+        /* only trigger bind() if port is not 0 (or --local is used) */
+        if (!streq(p[1], "0"))
+        {
+            options->ce.local_port_defined = true;
+        }
         options->ce.local_port = p[1];
     }
     else if (streq(p[0], "rport") && p[1] && !p[2])

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/916?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1976307a7643c82f31d55ca32c79cbe64b6fffc6
Gerrit-Change-Number: 916
Gerrit-PatchSet: 2
Gerrit-Owner: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: ordex <[email protected]...>
Gerrit-MessageType: newpatchset

#7 lore raw
In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/916?usp=email )

Change subject: Make 'lport 0' no longer sufficient to do '--bind'.
......................................................................

Make 'lport 0' no longer sufficient to do '--bind'.

'lport <anything>' used to trigger 'do socket bind', which is not useful in itself for the 'lport 0' case (port 0 -> OS assigns a random port, as it is done for unbound sockets) unless also binding to a particular local IP address ('--local 192.0.2.1').

The trigger for 'lport has been used, do socket bind' is ce.local_port_defined -> change the code to test for "0", and only set this for non-0 ports (NOTE: this is a string match, so if you really really want the old "lport 0" behaviour, using "lport 00" still does that...).

The ce.local_port value is still set, so '--lport 0' together with '--local 192.0.2.1' will give you a random port number bound to that IP address - without 'lport 0' it would default to 1194 or the value of '--port' (if not using '--rport').

Summary: socket bind is now only done if one of these is set
- --lport <port> with <port> not "0"
- --bind (default on the client is "--nobind")
- --local <address>

Github: schwabe/ics-openvpn#1794

Change-Id I1976307a7643c82f31d55ca32c79cbe64b6fffc6

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg31222.html

---
M doc/man-sections/link-options.rst
M src/openvpn/options.c

2 files changed, 9 insertions(+), 2 deletions(-)
doc/man-sections/link-options.rst +3 -1
index d48021e..287473e 100644
@@ -122,7 +122,9 @@
 
 --lport port
   Set default TCP/UDP port number. Cannot be used together with
-  ``--nobind`` option.
+  ``--nobind`` option.  A port number of ``0`` is only honoured to
+  achieve "bind() to a random assigned port number" if a bind-to IP
+  address is specified with ``--local``.
 
 --mark value
   Mark encrypted packets being sent with ``value``. The mark value can be
src/openvpn/options.c +6 -1
index ab56609..99dd60a 100644
@@ -6710,7 +6710,12 @@
     else if (streq(p[0], "lport") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
-        options->ce.local_port_defined = true;
+
+        /* only trigger bind() if port is not 0 (or --local is used) */
+        if (!streq(p[1], "0"))
+        {
+            options->ce.local_port_defined = true;
+        }
         options->ce.local_port = p[1];
     }
     else if (streq(p[0], "rport") && p[1] && !p[2])

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/916?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1976307a7643c82f31d55ca32c79cbe64b6fffc6
Gerrit-Change-Number: 916
Gerrit-PatchSet: 2
Gerrit-Owner: cron2 <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: ordex <[email protected]...>
Gerrit-MessageType: merged